package com.example.coding_problem;

import android.content.Context;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.SlidingPaneLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.coding_problem.Fragment.Environmental_indicators;
import com.example.coding_problem.Fragment.Code3;
import com.example.coding_problem.Fragment.High_speed;
import com.example.coding_problem.Fragment.Life_assistant;
import com.example.coding_problem.Fragment.Threshold_setting;
import com.example.coding_problem.Fragment.Vehicle_violation;
import com.example.coding_problem.Fragment.Road_condition_query;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {
    private ImageView iv_menu;
    private ListView lv_menu;
    private ImageView iv_top_menu_1;
    private TextView tv_top_menu;
    private TextView tv_top_menu_one;
    private TextView tv_top_menu_two;
    private LinearLayout LL_or;
    public Context context = this;
    //侧滑面版布局
    private PagerEnabledSlidingPaneLayout SP_sl;
    private ArrayList<HashMap<String, Integer>> actionlist = new ArrayList<HashMap<String, Integer>>();
    private SimpleAdapter actionAdapter;
    private FragmentManager fm;
    private FragmentTransaction ft;
    private SlidingPaneLayout SP_s1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    //在按一次退出功能
    public boolean onKeyDown(int KeyCode, KeyEvent event) {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            exitTime = System.currentTimeMillis();
            Toast.makeText(this, "在按一次退出", Toast.LENGTH_SHORT).show();
        } else {
            mHandler.postDelayed(mFinish, 0);
        }
        return false;
    }
    private long exitTime = 0;

    private Handler mHandler = new Handler();
    private Runnable mFinish = new Runnable() {
        @Override
        public void run() {
            finish();
        }
    };

    //
    private void initView() {
        iv_menu = (ImageView) findViewById(R.id.iv_menu);
        lv_menu = (ListView) findViewById(R.id.lv_menu);
        iv_top_menu_1 = (ImageView) findViewById(R.id.iv_top_menu_1);
        tv_top_menu = (TextView) findViewById(R.id.tv_top_menu);
        tv_top_menu_one = (TextView) findViewById(R.id.tv_top_menu_one);
        tv_top_menu_two = (TextView) findViewById(R.id.tv_top_menu_two);
        LL_or = (LinearLayout) findViewById(R.id.LL_or);
        SP_sl =findViewById(R.id.SP_s1);
        fm = getSupportFragmentManager();
        //顶部菜单栏
        iv_top_menu_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (SP_sl.isOpen()){
                    SP_sl.closePane();//关闭
                }else{
                    SP_sl.openPane();
                    lv_menu.setVisibility(View.VISIBLE);
                }
            }
        });
        //侧边菜单栏
        iv_menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (SP_sl.isOpen()){
                    SP_sl.closePane();
                }
            }
        });
        //侧边栏名称
        final String[] actionText = {
//                "图片",
//                "视频",
                "车辆违章",
                "环境指标",
                "公交车查询",
                "车辆违章查询",
                "路况查询",
                "生活助手",
                "高速ETC"
        };
        //侧滑栏图片
        int[] actionIcon = {
//                R.mipmap.ic_launcher,
//                R.mipmap.ic_launcher,
                R.mipmap.ic_launcher,
                R.mipmap.ic_launcher,
                R.mipmap.ic_launcher,
                R.mipmap.ic_launcher,
                R.mipmap.ic_launcher,
                R.mipmap.ic_launcher,
                R.mipmap.ic_launcher,
        };
        actionlist = new ArrayList<>();
        actionAdapter = new SimpleAdapter(MainActivity.this, actionlist, R.layout.menu_size, new String[]{"Text", "Icon"},
                new int[]{R.id.size_text, R.id.size_icon}
        );
        for (int i = 0; i < actionText.length; i++) {
            HashMap map = new HashMap();
            map.put("Text", actionText[i]);
            map.put("Icon", actionIcon[i]);
            actionlist.add(map);
        }
        //侧边栏点击跳转
        lv_menu.setAdapter(actionAdapter);
        lv_menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    if (i == 0) {
                    ft = fm.beginTransaction();
                    ft.replace(R.id.LL_or, new Code3());
                    ft.commit();
                    SP_sl.closePane();
                }else if (i == 1) {
                    ft = fm.beginTransaction();
                    ft.replace(R.id.LL_or, new Environmental_indicators());
                    ft.commit();
                    SP_sl.closePane();
                    lv_menu.setVisibility(View.GONE);
                }else if (i == 2) {
                        ft = fm.beginTransaction();
                        ft.replace(R.id.LL_or, new Threshold_setting());
                        ft.commit();
                        SP_sl.closePane();
                        lv_menu.setVisibility(View.GONE);
                    }else if (i == 3) {
                        ft = fm.beginTransaction();
                        ft.replace(R.id.LL_or, new Vehicle_violation());
                        ft.commit();
                        SP_sl.closePane();
                        lv_menu.setVisibility(View.GONE);
                    }else if (i == 4) {
                        ft = fm.beginTransaction();
                        ft.replace(R.id.LL_or, new Road_condition_query());
                        ft.commit();
                        SP_sl.closePane();
                        lv_menu.setVisibility(View.GONE);
                    }else if (i == 5) {
                        ft = fm.beginTransaction();
                        ft.replace(R.id.LL_or, new Life_assistant());
                        ft.commit();
                        SP_sl.closePane();
                        lv_menu.setVisibility(View.GONE);
                    }else if (i == 6) {
                        ft = fm.beginTransaction();
                        ft.replace(R.id.LL_or, new High_speed());
                        ft.commit();
                        SP_sl.closePane();
                        lv_menu.setVisibility(View.GONE);
                    }
            }
        });
    }
}
